<?php

namespace Houdunren\WeChat\Message;
use Illuminate\Support\Facades\Log;

trait Send
{

    public function text(string $content)
    {

        $xml = <<<php
<xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[%s]]></Content>
</xml>
php;
        return sprintf($xml, $this->FromUserName, $this->ToUserName, time(), $content);
    }
//@ param 二维数组参数
    public function news(array $data)
    {
        $news = '';
        $xml = <<<php
<xml>
  <ToUserName><![CDATA[%s]]></ToUserName>
  <FromUserName><![CDATA[%s]]></FromUserName>
  <CreateTime>%s</CreateTime>
  <MsgType><![CDATA[news]]></MsgType>
  <ArticleCount>%s</ArticleCount>
  <Articles>
  %s
  </Articles>
</xml>
php;
        $articleXml = <<<item
  <item>
      <Title><![CDATA[%s]]></Title>
      <Description><![CDATA[%s]]></Description>
      <PicUrl><![CDATA[%s]]></PicUrl>
      <Url><![CDATA[%s]]></Url>
    </item>
item;
        foreach ($data as $article) {
            $news .= sprintf($articleXml, $article['title'], $article['description'], $article['picurl'], $article['url']);
        }

        return sprintf($xml, $this->FromUserName, $this->ToUserName, time(), count($data), $news);
    }


}
